﻿package cn.flashk.map
{
    import cn.flashk.events.*;
    import cn.flashk.image.*;
    import cn.flashk.net.*;
    import flash.display.*;
    import flash.events.*;
    import flash.filters.*;
    import flash.geom.*;
    import flash.utils.*;

    public class Background extends Bitmap
    {
        private var smallFileURL:String;
        private var loaderTypeName:String = "mapBackground";
        private var smallBD:BitmapData;
        private var smallFixBD:BitmapData;
        private var data:XML;
        private var newX:Number;
        private var newY:Number;
        private var gridWidth:Number;
        private var gridHeight:Number;
        private var maxW:int;
        private var maxH:int;
        private var needShows:Array;
        private var loadedCout:uint;
        private var gridNumW:uint;
        private var gridNumH:uint;
        private var bdBak:BitmapData;
        public static var scaleFilter:BitmapFilter = new BlurFilter(2, 2, 1);
        public static var scaleSmooth:Boolean = false;

        public function Background()
        {
            LoaderManager.getInstance().addEventListener(LoaderManagerEvent.COMPLETE, checkFile);
            needShows = new Array();
            opaqueBackground = 0;
            return;
        }// end function

        public function init(param1:XML) : void
        {
            data = param1;
            loadedCout = 0;
            gridWidth = Number(data.skin.gridWidth);
            gridHeight = Number(data.skin.gridHeight);
            gridNumW = Math.ceil(MapEngine.getInstance().mapWidth / gridWidth);
            gridNumH = Math.ceil(MapEngine.getInstance().mapHeight / gridHeight);
            smallFileURL = data.skin.floder.toString() + data.skin.beginString + data.skin.smallFileName + data.skin.endString + data.skin.fileType;
            LoaderManager.getInstance().load(smallFileURL, loaderTypeName);
            smallFixBD = new BitmapData(MapEngine.getInstance().mapWidth, MapEngine.getInstance().mapHeight, false, 0);
            return;
        }// end function

        public function moveTo(param1:Number, param2:Number) : void
        {
            var _loc_11:int = 0;
            var _loc_12:Number = NaN;
            var _loc_13:String = null;
            var _loc_3:* = MapEngine.getInstance().getPoint(1);
            var _loc_4:* = int(_loc_3.x / gridWidth);
            var _loc_5:* = int(_loc_3.y / gridHeight);
            maxW = Math.ceil(MapEngine.getInstance().viewSize.width / gridWidth);
            maxH = Math.ceil(MapEngine.getInstance().viewSize.height / gridHeight);
            var _loc_6:* = Math.ceil(maxW / 2) + _loc_4;
            var _loc_7:* = Math.ceil(maxH / 2) + _loc_5;
            needShows = new Array();
            var _loc_8:* = gridNumW;
            var _loc_9:* = gridNumH;
            var _loc_10:int = 1;
            while (_loc_10 <= _loc_8)
            {
                
                _loc_11 = 1;
                while (_loc_11 <= _loc_9)
                {
                    
                    _loc_12 = Math.sqrt(Math.pow(_loc_10 - _loc_6, 2) + Math.pow(_loc_11 - _loc_7, 2));
                    needShows.push({range:_loc_12, x:_loc_10, y:_loc_11});
                    _loc_11++;
                }
                _loc_10++;
            }
            needShows.sortOn("range", Array.NUMERIC);
            _loc_10 = 0;
            while (_loc_10 < needShows.length)
            {
                
                _loc_13 = data.skin.floder.toString() + data.skin.beginString + needShows[_loc_10].y + data.skin.spaceString + needShows[_loc_10].x + data.skin.endString + data.skin.fileType;
                needShows[_loc_10].fileURL = _loc_13;
                LoaderManager.getInstance().add(_loc_13, loaderTypeName);
                _loc_10++;
            }
            return;
        }// end function

        public function copyObjectView(param1:BitmapData, param2:Rectangle, param3:Point) : void
        {
            smallFixBD.copyPixels(param1, param2, param3, null, null, true);
            return;
        }// end function

        private function checkFile(event:LoaderManagerEvent) : void
        {
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            var _loc_4:Loader = null;
            if (event.typeName == loaderTypeName)
            {
                if (event.fileURL == smallFileURL)
                {
                    ImageDecode.toBitmapData(event.data).contentLoaderInfo.addEventListener(Event.INIT, initSmallBD);
                }
                _loc_2 = -1;
                _loc_3 = 0;
                while (_loc_3 < needShows.length)
                {
                    
                    if (needShows[_loc_3].fileURL == event.fileURL)
                    {
                        _loc_2 = _loc_3;
                    }
                    _loc_3++;
                }
                if (_loc_2 != -1)
                {
                    _loc_4 = ImageDecode.toBitmapData(event.data);
                    _loc_4.name = "gridLoader-" + needShows[_loc_2].x + "-" + needShows[_loc_2].y;
                    _loc_4.contentLoaderInfo.addEventListener(Event.INIT, initGridBD);
                }
            }
            return;
        }// end function

        private function initSmallBD(event:Event) : void
        {
            var _loc_2:* = LoaderInfo(event.currentTarget).loader;
            smallBD = Bitmap(_loc_2.content).bitmapData.clone();
            LoaderInfo(event.currentTarget).removeEventListener(Event.INIT, initSmallBD);
            Bitmap(_loc_2.content).bitmapData.dispose();
            _loc_2.unload();
            var _loc_3:* = new Matrix();
            _loc_3.scale(Number(data.skin.samllPixel), Number(data.skin.samllPixel));
            smallFixBD.draw(smallBD, _loc_3, null, null, null, scaleSmooth);
            if (scaleFilter != null)
            {
                smallFixBD.applyFilter(smallFixBD, new Rectangle(0, 0, smallFixBD.width, smallFixBD.height), new Point(0, 0), scaleFilter);
            }
            this.bitmapData = smallFixBD;
            moveTo(MapEngine.getInstance().centerX, MapEngine.getInstance().centerY);
            return;
        }// end function

        private function initGridBD(event:Event) : void
        {
            var _loc_7:int = 0;
            var _loc_2:* = LoaderInfo(event.currentTarget).loader;
            var _loc_3:* = Bitmap(_loc_2.content).bitmapData;
            LoaderInfo(event.currentTarget).removeEventListener(Event.INIT, initGridBD);
            var _loc_4:* = _loc_2.name.split("-");
            var _loc_5:* = int(_loc_4[1]) - 1;
            var _loc_6:* = int(_loc_4[2]) - 1;
            smallFixBD.copyPixels(_loc_3, new Rectangle(0, 0, _loc_3.width, _loc_3.height), new Point(_loc_5 * gridWidth, _loc_6 * gridHeight), null, null, false);
            _loc_3 = null;
            Bitmap(_loc_2.content).bitmapData.dispose();
            _loc_2.unload();
            var _loc_9:* = loadedCout + 1;
            loadedCout = _loc_9;
            if (loadedCout == gridNumW * gridNumH)
            {
                _loc_7 = getTimer();
                this.dispatchEvent(new Event("gridAllLoaded"));
                trace(getTimer() - _loc_7 + "+++++");
            }
            return;
        }// end function

    }
}
